package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapBookDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.VocService;
import jakarta.annotation.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("admin/voc")
public class VocController {
    @Resource
    VocService vocService;

    @GetMapping("list")
    public Result<PageDTO<VocDTO>> getVocList(VocPageRequest pageQuery) {
        PageDTO<VocDTO> vocabularies = vocService.getVocabularies(pageQuery);

        return Result.ok(vocabularies);
    }


    @PostMapping()
    public Result<Void> addVoc(@RequestBody VocCommand command) {
        vocService.addVoc(command);
        return Result.ok();
    }


    @PostMapping("file")
    public Result<String> addVoice(@RequestParam("file") MultipartFile file) throws IOException {
        String url = vocService.uploadsFile(file);
        return Result.ok(url);
    }

    @PutMapping()
    public Result<Void> updateVoc(@RequestBody VocCommand command) {
        vocService.updateVoc(command);
        return Result.ok();
    }

    @GetMapping("relations/{vocId}")
    public Result<List<VocChapBookDTO>> getVocRelations(@PathVariable("vocId") Long vocId) {
        List<VocChapBookDTO> vocRelations = vocService.getVocRelations(vocId);
        return Result.ok(vocRelations);
    }

    @GetMapping("template")
    public ResponseEntity<org.springframework.core.io.Resource> downloadTemplate() {
        ClassPathResource template = new ClassPathResource("templates/单词上传模版.xlsx");
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=" + template.getFilename())
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .body(template);
    }


    @DeleteMapping("relation/{vocChapBookId}")
    public Result<Void> deleteVocRelation(@PathVariable("vocChapBookId") Long vocChapBookId) {
        vocService.deleteVocRelation(vocChapBookId);
        return Result.ok();
    }


    @DeleteMapping("interpretation/{vocMeaningId}")
    public Result<Void> deleteVocMeaning(@PathVariable("vocMeaningId") Long vocMeaningId) {
        vocService.deleteInterpretation(vocMeaningId);
        return Result.ok();
    }

    @DeleteMapping("sentence/{sentenceId}")
    public Result<Void> deleteVocExample(@PathVariable("sentenceId") Long sentenceId) {
        vocService.deleteExample(sentenceId);
        return Result.ok();
    }

    @DeleteMapping("file/{type}/{id}")
    public Result<Void> deleteVocFile(@PathVariable("id") Long id, @PathVariable("type") String type, String name) {
        vocService.deleteFile(id, type, name);
        return Result.ok();
    }

    // 删除指定单词

    @DeleteMapping("/{vocId}")
    public Result<Void> deleteVocabulary(@PathVariable("vocId") Long vocId) {
        vocService.deleteVocabulary(vocId);
        return Result.ok();
    }

    @PostMapping("excel")
    public Result<Void> addVocExcel(@RequestParam("vocExcel") MultipartFile file) throws IOException {
        String loginId = (String) StpKit.ADMIN.getLoginId();
        vocService.addVocExcel(file, Long.parseLong(loginId));
        return Result.ok();
    }


    @PostMapping("excel/validate")
    public Result<Void> vocExcelValidate(@RequestParam("vocExcel") MultipartFile file) throws IOException {
        vocService.vocExcelValidate(file);
        return Result.ok();
    }


    @DeleteMapping()
    public Result<Void> deleteVocabulary(@RequestBody BatchDeleteCommand command) {
        vocService.deleteVocabularies(command);
        return Result.ok();
    }

}
